<template>
	<view>


		<view class="header" @click="goBack">
			<view class="arrow">
			</view>
			<view class="title" style="color: #000000;">
				分类
			</view>
		</view>

		<view class="" style="padding: 10rpx;">
			<u-search placeholder="搜索商品名称" @clear="clearSearch" @change="getGoodsListFrom1" v-model="keyword"
				:show-action="fasle"></u-search>
		</view>


		<u-tabs :list="listFrom" bg-color="#fff" :current="current" active-color="#7578EF" :is-scroll="false"
			@change="change">
		</u-tabs>


		<view v-if="current == 0" v-for="(item, index) in textListFrom" :key="index"
			class="flex align-center margin-top u-border-bottom wh96 r10"
			style="padding: 20rpx; margin: 15rpx auto 0;background-color: #FFFFFF;"
			@click="inputNum(item)">
			<image class="w150 h150" :src="img + item.image" mode=""></image>
			<view class="padding-left" style="line-height: 60rpx;">
				<view>{{item.name}}</view>
				<view>单位：{{item.unit_name}}</view>
			</view>
		</view>
		<view v-if="current == 1" class="text-tab">
			<view class="box">
				<view class="marige-bor" v-for="(item, index) in textListFromType" :key="index">
					<view class="diso-flex" @tap="getGoodsListFrom2(item.id)">
						<view class="">
							{{item.type_name}}
						</view>
						<view class="conter-image">
						</view>
					</view>
				</view>
			</view>
		</view>






		<u-popup v-model="show" mode="center" width="500rpx" height="160px" border-radius="20">
			<view class="show" style="height: 60rpx; line-height: 60rpx; padding-top: 20rpx;">
				{{textName}}
			</view>
			<view class="show-poup flex-box">
				<text>数量</text>
				<input type="number" v-model="num" placeholder="请输入" />
			</view>
			<view class="flex-around add-clas">
				<view class="" style="background-color: #CCCCCC;" @click="show = false">
					取消
				</view>
				<view class="bg-gradual-blue" @click="addInpput">
					确定
				</view>
			</view>
		</u-popup>
		
		
		<!-- 信息提示-->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				tabCur: 0,
				mainCur: 0,
				verticalNavTop: 0,
				load: true,
				show: false,
				memo: '',
				num: '',
				img: this.$images,
				stockId: '',
				goodsList: [],
				inofId: '',
				itemName: '',
				textName: '',
				keyword: '',
				listFrom: [{
					name: '全部'
				}, {
					name: '筛选'
				}],
				current: 0,
				textListFromType: [],
				textListFrom: [],
				pagesize: 15,
				page: 1,
			};
		},
		onLoad(e) {
			this.getGoodsListFrom()
			console.log(this.img)
		},
		onReachBottom() {
			this.page++
			this.getGoodsListFrom()
		},
		methods: {
			change(index) {
				console.log(index)
				this.current = index;
				this.textListFrom = []
				this.textListFromType = []
				this.page = 1
				this.getGoodsListFrom()
			},

			getGoodsListFrom2(id) {
				this.danweId = id
				
				this.textListFrom = []
				this.textListFromType = []
				this.current = 0
				this.getGoodsListFrom()
			},

			async getGoodsListFrom() {

				const res = await this.$u.post("api/Invoic/goods_find", {
					page: this.page,
					pagesize: this.pagesize,
					type_id: this.danweId ? this.danweId : '',
					// classify_id: this.danweId ? this.danweId : '',
					search: this.keyword ? this.keyword : '',
				});
				this.danweId = ''
				this.popupShow3 = false
				this.textListFrom = [...this.textListFrom, ...res.data.goods_list]
				this.textListFromType = [...this.textListFromType, ...res.data.goods_type]
			},


			getGoodsListFrom1(id) {
				this.textListFrom = []
				this.textListFromType = []
				this.current = 0
				this.getGoodsListFrom()

			},

			clearSearch() {
				this.getGoodsListFrom()
			},
			
			// 确定我的入库pages/removeStock/index
			addInpput() {
				if (this.num == '') return this.$refs.uToast.show({ title: '数量不能未空', type: 'error' });
				this.show = false
				var obj = {
					num: this.num,
					goods_id: this.inofId,
					name: this.itemName.name,
					image: this.itemName.image,
					unit: this.itemName.unit_name,
				}
				
				// createtime: 1623085395
				// id: 3
				// image: "/uploads/20210608/a3ae3744da4fde80445feb88776f9a75.jpg"
				// name: "测试桁架"
				// remark: "测试"
				// type_id: 1
				// unit_id: 1
				

				this.$refs.uToast.show({ title: '保存成功', type: 'success' });

				this.goodsList.push(obj)
				uni.setStorageSync('navigatorProp', 2)
				uni.setStorageSync('goodsList', this.goodsList)
				console.log("goodsList ", this.goodsList)
			},


			inputNum(item) {
				this.memo = ''
				this.num = ''
				this.show = true
				this.inofId = item.id
				this.itemName = item
				this.textName = item.name

				console.log(item)
				
			},


			goBack(id) {
				console.log(id)
				uni.navigateBack({
					url: '/pages/putStock/addShop?navigatorProp=' + 4
				})

				// let pages = getCurrentPages(); //获取所有页面栈实例列表
				// let nowPage = pages[pages.length - 1]; //当前页页面实例
				// let prevPage = pages[pages.length - 2]; //上一页页面实例
				// prevPage.$vm.godList = JSON.stringify(this.goodsList);
				// prevPage.$vm.listFrom = this.goodsList //修改上一页data里面的searchVal参数值为1211
				// // prevPage.$vm.godList = this.goodsList 
				// console.log('this.goodsList', this.goodsList)

			},

			// 获取;列表
			async getList() {
				const res = await this.$u.post("api/jinxiao.Goods/index", {});
				this.list = res.data
			},





			TabSelect(e) {
				this.tabCur = e.currentTarget.dataset.id;
				this.mainCur = e.currentTarget.dataset.id;
				this.verticalNavTop = (e.currentTarget.dataset.id - 1) * 50;
			},
			VerticalMain(e) {
				// #ifdef MP-ALIPAY
				return false; //支付宝小程序暂时不支持双向联动
				// #endif
				let that = this;
				let tabHeight = 0;
				if (this.load) {
					for (let i = 0; i < this.list.length; i++) {
						let view = uni.createSelectorQuery().select('#main-' + this.list[i].id);
						view.fields({
								size: true,
							},
							data => {
								this.list[i].top = tabHeight;
								// tabHeight = tabHeight + data.height;
								// this.list[i].bottom = tabHeight;
							},
						).exec();
					}
					this.load = false;
				}
				let scrollTop = e.detail.scrollTop + 10;
				for (let i = 0; i < this.list.length; i++) {
					if (scrollTop > this.list[i].top && scrollTop < this.list[i].bottom) {
						this.verticalNavTop = (this.list[i].id - 1) * 50;
						this.tabCur = this.list[i].id;
						console.log(scrollTop);
						return false;
					}
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	.header {
		width: 100%;
		height: 150rpx;
		line-height: 150rpx;
		display: flex;
		align-items: center;
		// padding-left: 10rpx;
		padding-top: 30rpx;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #000000;
		background: #EEEEEE;

		.arrow {
			position: relative;
			display: inline-block;
		}

		.arrow::before {
			content: '';
			width: 10px;
			height: 10px;
			border: 0px;
			border-bottom: solid 2px #000;
			border-left: solid 2px #000;
			-ms-transform: rotate(45deg);
			-webkit-transform: rotate(45deg);
			transform: rotate(45deg);
			position: absolute;
			top: 50%;
			left: 0;
			margin-top: -4px;
			margin-left: 28rpx;
		}

		.title {
			color: #fff;
			margin: 0 auto;
		}
	}





	.fixed {
		position: fixed;
		z-index: 99;
	}

	.VerticalNav.nav {
		width: 200upx;
		white-space: initial;
	}

	.VerticalNav.nav .cu-item {
		width: 100%;
		text-align: center;
		background-color: #fff;
		margin: 0;
		border: none;
		height: 50px;
		position: relative;
	}

	.VerticalNav.nav .cu-item.cur {
		background-color: #f1f1f1;
	}

	.VerticalNav.nav .cu-item.cur::after {
		content: '';
		width: 8upx;
		height: 30upx;
		border-radius: 10upx 0 0 10upx;
		position: absolute;
		background-color: currentColor;
		top: 0;
		right: 0upx;
		bottom: 0;
		margin: auto;
	}

	.VerticalBox {
		display: flex;
	}


	.text-tab {
		width: 100%;
		// padding: 30rpx;
		padding-bottom: 0;


		.box {
			width: 98%;
			margin: 15rpx auto 0;
			background-color: #FFFFFF;
			border-radius: 10rpx;
		}

		.marige-bor {
			width: 90%;
			margin: 0 auto;
			color: #333333;
			display: flex;
			font-size: 28rpx;
			align-items: center;
			border-bottom: 1rpx solid #F0F0F0;
			justify-content: space-between;

			.diso-flex {
				width: 100%;
				height: 80rpx;
				display: flex;
				align-items: center;
				background-color: #FFFFFF;
				justify-content: space-between;
			}

			.conter-image {
				width: 22rpx;
				height: 36rpx;
				font-weight: 700;
				color: #999;
			}
		}

		.marige-bor:last-child {
			border-bottom: 0;
		}
	}


	.VerticalMain {
		background-color: #f1f1f1;
		flex: 1;
	}


	.show-poup {
		width: 80%;
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 10rpx;
		margin: 36rpx auto;
		padding-left: 30rpx;
		background-color: #F5F5F5;
	}

	.add-clas {
		width: 100%;
		height: 60rpx;

		view {
			width: 30%;
			height: 60rpx;
			color: #FFFFFF;
			line-height: 60rpx;
			border-radius: 30rpx;
			text-align: center;
		}
	}
</style>
